package org.eoti.game.roshambo;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class DefaultRuleMonger
extends RuleMonger<DefaultWeapon, String, DefaultPlayer>
{
	private enum Rule
	{
		Rock(DefaultWeapon.Scissors, "Rock smashes Scissors"),
		Paper(DefaultWeapon.Rock, "Paper covers Rock"),
		Scissors(DefaultWeapon.Paper, "Scissors cut Paper");
		Rule(DefaultWeapon beats, String desc)
		{
			this.beats = beats;
			this.desc = desc;
		}
		private DefaultWeapon beats;
		private String desc;
		public boolean beats(DefaultWeapon test){return beats == test;}
		public String getDescription(){return desc;}
	}

	public Judgement<DefaultWeapon, String, DefaultPlayer> judge(Challenge<DefaultWeapon, String, DefaultPlayer> challenge)
	{
		DefaultWeapon w1 = challenge.getPlayer1Weapon();
		DefaultWeapon w2 = challenge.getPlayer2Weapon();
		if(w1 == w2)
			return new Judgement<DefaultWeapon,String,DefaultPlayer>(challenge, Judgement.Winner.Tie, "It's a tie!");

		Rule rule = Rule.valueOf( w1.name() );
		if(rule.beats( w2 ))
			return new Judgement<DefaultWeapon,String,DefaultPlayer>(
					challenge,
					Judgement.Winner.Player1,
			    rule.getDescription()
			);

		rule = Rule.valueOf( w2.name() );
		if(rule.beats( w1 ))
			return new Judgement<DefaultWeapon,String,DefaultPlayer>(
					challenge,
					Judgement.Winner.Player2,
			    rule.getDescription()
			);

		throw new IllegalArgumentException(String.format("Someone cheated! %s can't play with %s", w1, w2));
	}
}
